package dk.ucn.houseenabler;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class OutdoorEnvironment extends Activity{
	private ListView list;
	private ArrayAdapter<String> adapter;
	private Button save_data_button;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_outdoor_environment);
        save_data_button = (Button)findViewById(R.id.btn_save_data);
        try{
            //Get Questions from XML File
              String[] CD = getResources().getStringArray(R.array.environmentalComponents);
            
        list = (ListView)findViewById(R.id.list);
        list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        
        adapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_multiple_choice,CD);
        list.setAdapter(adapter);
        list.setLongClickable(true);
        list.setOnItemLongClickListener (new OnItemLongClickListener() {
        	  public boolean onItemLongClick(AdapterView parent, View view, int position, long id) {
        		  		String text = ((TextView) view).getText().toString();
						Toast.makeText(getParent().getApplicationContext(),text , Toast.LENGTH_LONG).show();
						return true;
        		  }
        		});
        
        
        save_data_button.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				//savedatahere
				byte[] answers = null;
				answers = new byte[161];
				int index = 0;
				
				
				while(index < answers.length)
				{
				if(list.isItemChecked(index))
				{
					answers[index] = 1;
					System.out.println("Index: " +index + " isItemChecked: " + list.isItemChecked(index));
				}else
				{
					answers[index] = 0;
					System.out.println("Index: " +index + " isItemChecked: " + list.isItemChecked(index));
				}
				index++;
				}
				
				byte type = 1;
				try {
					FileHandler.exportToFile(MainActivity.getSubjectName(), answers, type);
					MainActivity.changeTab(0);
				} catch (Exception ex) {
					// TODO Auto-generated catch block
					Toast.makeText(getParent().getApplicationContext(), "An error has occured, check that a name has been saved!", Toast.LENGTH_LONG).show();
					ex.printStackTrace();
				}
				
				
			}
		});
        
    
       }catch(Exception ex){
    	   System.out.println("ABC + " + ex.getMessage());
       }
       
       
    }

	
}
	

